package com.sunday.pinxinghui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;
import com.sunday.pinxinghui.R;

import java.io.File;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 *
 */

public class GradImageAdapter extends BaseAdapter {
    private View.OnClickListener onClickListener;
    private List<String> list;
    private int parentPosition;

    public GradImageAdapter(int max , List<String> list ,int parentPosition) {
        this.max = max;
        this.list = list;
        this.parentPosition = parentPosition;
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    private int max;

    @Override
    public int getCount() {
        if(list.size() >= max){
            return max;
        }
        return list.size() +1;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(viewHolder == null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_grid_image , null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder)convertView.getTag();
        }

        viewHolder.photo_add.setTag(R.id.photo_add ,parentPosition);
        viewHolder.photo_add.setTag(position);

        viewHolder.photo_delete.setTag(R.id.photo_delete ,parentPosition);
        viewHolder.photo_delete.setTag(position);

        if(position != list.size()){
            File file = new File(list.get(position));
            if (file.exists()) {
                Picasso.with(parent.getContext())
                        .load(file)
                        .placeholder(R.drawable.default_error)
                        .into(viewHolder.photo_add);
            } else {
                Picasso.with(parent.getContext())
                        .load(list.get(position))
                        .placeholder(R.drawable.default_error)
                        .into(viewHolder.photo_add);
            }
        }else {
            Picasso.with(parent.getContext())
                    .load("http")
                    .error(R.mipmap.add_photo)
                    .placeholder(R.mipmap.add_photo)
                    .into(viewHolder.photo_add);
        }


        viewHolder.photo_delete.setVisibility(position == list.size()?View.GONE:View.VISIBLE);
        return convertView;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.photo_add)
        ImageView photo_add;
        @Bind(R.id.photo_delete)
        ImageView photo_delete;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            photo_delete.setOnClickListener(onClickListener);
            photo_add.setOnClickListener(onClickListener);
        }

    }
}

